#include "inc_test.h"

// #define MIN_STACKSIZE   (4096) /* 注意redzone,*/
#define TEST_NR		 (1000000)

CROUT_STRUCT *crs[1];
typedef struct{
   unsigned long long val; 
   unsigned long long sum;
}arg_t;
/* 计算和 */
static void *rout_generator(void *arg){
	unsigned long long sum = 0;
	arg_t *args = (arg_t *)arg;
	for (word_t i = 0; i < TEST_NR; i++){
		CROUT_YIELD(crs[0]);
		args->sum += atm_ld(&args->val);
	}
	return (void *)sum;
}

arg_t glbarg = {0};
void test_generator(){
	CROUT_ATTR test_attr;
	test_attr.share_stack = NULL;
	struct timespec ts;
	struct timespec dt;
	word_t cycles0;
	word_t cycles1;
	word_t time = TEST_NR;
	test_attr.stack_size = MIN_STACKSIZE;
	if (0 != CROUT_CREATE(&crs[0], &test_attr, rout_generator, (void *)&glbarg)){
		printf("create failed\n");
		return;
	}
	TIME_STAMP(&ts);
	cycles0 = rdcnt_relax();
	for(unsigned long idx = 0; idx < time; idx++){
		atm_st(&glbarg.val, idx);
		CROUT_RESUME(crs[0]);
	}
	cycles1 = rdcnt_relax();
	TIME_STAMP(&dt);
	printf("[%8llu ns/ops] %lu generator time in us: %llu (%lld cycles/op), result %llu\n", \
			TIME_DELTA_NS(&ts, &dt)/time, (unsigned long)time, TIME_DELTA_US(&ts, &dt), \
			((llong_t)cycles1-cycles0)/TEST_NR, atm_ld(&glbarg.sum));

	for(unsigned long idx = 0; idx < 1; idx++){
		CROUT_RELEASE(crs[idx]);
	}
	fprintf(stderr, "%llu\n", TIME_DELTA_US(&ts, &dt));
}

int main(){
	test_generator();
	return 0;
}
